/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name:login.c
> Author:xxx
> Mail: 
> Created Time: 2024年12月04日 星期三 11时59分19秒
***************************************************/

#include "cJSON.h"
#include "login.h"

//管理员、员工、家属登陆
int login(int client_socket,int num){
	char username[20] = {0};
	char passwd[20] = {0};
	printf("input gly账号:");
	scanf("%s",username);
	printf("input gly密码:");
	scanf("%s",passwd);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	cJSON* type;
	if(num == 1){
		type = cJSON_CreateString("gly_login");//管理员登陆
	}else if(num == 2){
		type = cJSON_CreateString("staff_login");//员工登陆
	}else{
		type = cJSON_CreateString("kin_login");//家属登陆
	}
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* passwd_0 = cJSON_CreateString(passwd);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"passwd",passwd_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}




//员工、家属注册
int register_0(int client_socket,int num){
	char username[20] = {0};
	char passwd[20] = {0};
	printf("input 用户名:");
	scanf("%s",username);
	printf("input 密码:");
	scanf("%s",passwd);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	cJSON* type;
	if(num == 1){
		type = cJSON_CreateString("staff_register");//员工注册
	}else{
		type = cJSON_CreateString("kin_register");//家属注册
	}
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* passwd_0 = cJSON_CreateString(passwd);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"passwd",passwd_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//删除离职人员、家属
int delete_0(int client_socket,int num){
	char username[20] = {0};
	printf("input 要删除的用户名:");
	scanf("%s",username);
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	
	cJSON* type ;
	if(num == 1){
		type = cJSON_CreateString("delete_nurse");
	}else{
		type = cJSON_CreateString("delete_kin");
	}
	cJSON* username_0 = cJSON_CreateString(username);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//员工、家属修改密码
int change_passwd(int client_socket,int num){
	char username[20] = {0};
	char passwd[20] = {0};
	char newpasswd[20] = {0};
	printf("input 用户名:");
	scanf("%s",username);
	printf("input 密码:");
	scanf("%s",passwd);
	printf("input 新密码:");
	scanf("%s",newpasswd);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	cJSON* type;
	if(num == 1){
		type = cJSON_CreateString("staff_change_passwd");//员工修改密码
	}else{
		type = cJSON_CreateString("kin_change_passwd");//家属修改密码
	}
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* passwd_0 = cJSON_CreateString(passwd);
	cJSON* newpasswd_0 = cJSON_CreateString(newpasswd);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"passwd",passwd_0);
	cJSON_AddItemToObject(root,"newpasswd",newpasswd_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//员工请假
int leave(int client_socket){
	char username[20] = {0};
	char reason[100] = {0};
	char start_time[20] = {0};
	char end_time[20] = {0};
	printf("input 姓名:");
	scanf("%s",username);
	printf("input 请假理由:");
	scanf("%s",reason);
	printf("input 开始时间:");
	scanf("%s",start_time);
	printf("input 结束时间:");
	scanf("%s",end_time);
	
	//创建json对象
	cJSON* root = cJSON_CreateObject();
	
	cJSON* type = cJSON_CreateString("staff_leave");
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* reason_0 = cJSON_CreateString(reason);
	cJSON* start_time_0 = cJSON_CreateString(start_time);
	cJSON* end_time_0 = cJSON_CreateString(end_time);
	
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"reason",reason_0);
	cJSON_AddItemToObject(root,"start_time",start_time_0);
	cJSON_AddItemToObject(root,"end_time",end_time_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//查看假条
int cat_leave(int client_socket){
	cJSON* root = cJSON_CreateObject();
	cJSON* type = cJSON_CreateString("cat_leave");
	cJSON_AddItemToObject(root,"type",type);
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
}

//管理员审批假条
int gly_examine(int client_socket){
	cat_leave(client_socket);
	char username[20] = {0};
	char examine[20] = {0};
	printf("input 要审批的员工姓名:");
	scanf("%s",username);
	printf("input 审批是否通过:");//批准/不批准
	scanf("%s",examine);
	cJSON* root = cJSON_CreateObject();
	cJSON* type = cJSON_CreateString("gly_examine");
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* examine_0 = cJSON_CreateString(examine);
	cJSON_AddItemToObject(root,"type",type);
	cJSON_AddItemToObject(root,"username",username_0);
	cJSON_AddItemToObject(root,"examine",examine_0);
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//登记老人档案
int write_files(int client_socket){
	char name[20] = {0};
	int age = 0;
	char sex[5] = {0};
	char id_number[20] = {0};
	char family_members[20] = {0};
	char phone[15] = {0};
	char health_status[20] = {0};
    char pervalence[50] = {0};
    int medicine_fee = 0;
	// 创建 JSON 对象
    cJSON* root = cJSON_CreateObject();

    printf("input 姓名: ");
    scanf("%s", name);
    printf("input 年龄: ");
    scanf("%d", &age);
    printf("input 性别: ");
    scanf("%s", sex);
    printf("input id_number: ");
    scanf("%s", id_number);
    printf("input 家属姓名: ");
    scanf("%s", family_members);
    printf("input phone: ");
    scanf("%s", phone);
	
	cJSON* type = cJSON_CreateString("write_files");
	cJSON* cjson_name = cJSON_CreateString(name);
	cJSON* cjson_age = cJSON_CreateNumber(age);
	cJSON* cjson_sex = cJSON_CreateString(sex);
	cJSON* cjson_id_number = cJSON_CreateString(id_number);
	cJSON* cjson_family_members = cJSON_CreateString(family_members);
    cJSON* cjson_phone = cJSON_CreateString(phone);
	
	printf("input 健康情况: ");
    scanf("%s", health_status); // 健康 / 患病
    cJSON* cjson_health_status = cJSON_CreateString(health_status);
	cJSON* cjson_pervalence;
	cJSON* cjson_medicine_fee;
    if(strcmp(health_status,"健康") == 0){
        cjson_pervalence = cJSON_CreateString("无疾病");
        cjson_medicine_fee = cJSON_CreateNumber(0);
    }else{
        printf("input 患病情况: ");
        scanf("%s", pervalence);
        cjson_pervalence = cJSON_CreateString(pervalence);
        printf("input 医药费: ");
        scanf("%d", &medicine_fee);
        cjson_medicine_fee = cJSON_CreateNumber(medicine_fee);
    }
	cJSON* cjson_month_fee = cJSON_CreateNumber(2000);
	
	cJSON_AddItemToObject(root,"type",type);
    cJSON_AddItemToObject(root, "name", cjson_name);
    cJSON_AddItemToObject(root, "age", cjson_age);
    cJSON_AddItemToObject(root, "sex", cjson_sex);
    cJSON_AddItemToObject(root, "id_number", cjson_id_number);
    cJSON_AddItemToObject(root, "family_members", cjson_family_members);
    cJSON_AddItemToObject(root, "phone", cjson_phone);
    cJSON_AddItemToObject(root, "health_status", cjson_health_status);
    cJSON_AddItemToObject(root, "pervalence", cjson_pervalence);
    cJSON_AddItemToObject(root, "medicine_fee",cjson_medicine_fee);
    cJSON_AddItemToObject(root, "month_fee", cjson_month_fee);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//删除老人档案
int delete_files(int client_socket){
	char name[20] = {0};
	printf("input 要删除的档案人名:");
	scanf("%s",name);
	
	// 创建 JSON 对象
    cJSON* root = cJSON_CreateObject();
	
	cJSON* type = cJSON_CreateString("delete_files");
	cJSON* cjson_name = cJSON_CreateString(name);
	
	cJSON_AddItemToObject(root, "type",type);
	cJSON_AddItemToObject(root, "name",cjson_name);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
}

//用户充值
int recharge(int client_socket){
	char username[20] = {0};
	int money = 0;
	printf("input 账号姓名:");
	scanf("%s",username);
	printf("input 充值金额:");
	scanf("%d",&money);
	
	// 创建 JSON 对象
    cJSON* root = cJSON_CreateObject();
	cJSON* type = cJSON_CreateString("kin_recharge");
	cJSON* username_0 = cJSON_CreateString(username);
	cJSON* money_0 = cJSON_CreateNumber(money);
	
	cJSON_AddItemToObject(root,"type",type);
    cJSON_AddItemToObject(root, "username", username_0);
    cJSON_AddItemToObject(root, "money", money_0);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

//创建用户账单
int creat_bill(int client_socket){
	char name[20] = {0};
	int medicine_fee = 0;
	int month_fee = 2000;
	int total_fee = 0;
	int owing  = 0;
	
	printf("input 用户姓名: ");
    scanf("%s", name);
	printf("input 用药费用: ");
    scanf("%d", &medicine_fee);
	total_fee = medicine_fee + month_fee;
	owing = total_fee;
	
	cJSON* root = cJSON_CreateObject();
	cJSON* type = cJSON_CreateString("creat_bill");
	cJSON* cjson_name = cJSON_CreateString(name);
	cJSON* cjson_medicine_fee = cJSON_CreateNumber(medicine_fee);
	cJSON* cjson_month_fee = cJSON_CreateNumber(month_fee);
	cJSON* cjson_total_fee = cJSON_CreateNumber(total_fee);
	cJSON* cjson_account_fee = cJSON_CreateNumber(0);//初始账户余额为0
	cJSON* cjson_owing = cJSON_CreateNumber(owing);
	cJSON* cjson_recent_time = cJSON_CreateString("0");
	
	cJSON_AddItemToObject(root, "type", type);
	cJSON_AddItemToObject(root, "name", cjson_name);
	cJSON_AddItemToObject(root, "medicine_fee", cjson_medicine_fee);
	cJSON_AddItemToObject(root, "month_fee", cjson_month_fee);
	cJSON_AddItemToObject(root, "total_fee", cjson_total_fee);
	cJSON_AddItemToObject(root, "account_fee", cjson_account_fee);
	cJSON_AddItemToObject(root, "owing", cjson_owing);
	cJSON_AddItemToObject(root, "recent_time", cjson_recent_time);
	
	char str[1024] = {0};
	strcpy(str,cJSON_Print(root));
	printf("%s\n",str);
	write(client_socket,str,strlen(str));
	memset(str,0,1024);
	read(client_socket,str,sizeof(str));
	printf("%s\n",str);
	if(strcmp(str,"success") == 0){
		return 1;
	}
	return 0;
}

















